home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
pt20pc.zip
/
WP.C
< prev
Wrap
C/C++ Source or Header
|
1991-02-04
|
18KB
|
717 lines
#include "pt.h"
#include "string.h"
#include "direct.h"
#define PATTERNSIZE 64
static unsigned char *endPattern;
static unsigned char *beginPattern;
static unsigned char filePatterns[PATTERNSIZE];
static unsigned char retFilename[PATTERNSIZE];
static int savedAttribute;
static int dirNameSize;
unsigned char * pascal
/* XTAG:findFirstPatternFile */
findFirstPatternFile(patterns, attribute, dta)
unsigned char *patterns;
int attribute;
unsigned char *dta;
{
unsigned char *fileName;
unsigned char *endPrefix;
int i;
/* save patterns for later use in findNextPatternFile */
strncpy(filePatterns, patterns, PATTERNSIZE);
savedAttribute = attribute;
beginPattern = filePatterns;
/* see if this pattern has at least two components */
endPattern = strchr(beginPattern, '|');
/* find the path prefix and isolate the final pathname component */
/* NOTE: this fails for full pathnames (starting with "\") */
/* start looking at the end of the pathname */
if( endPattern != NULL ) {
*endPattern = '\0';
endPrefix = endPattern - 1;
} else
endPrefix = beginPattern + strlen(filePatterns) - 1;
/* look until you see a "\" or a ":" */
for( ; endPrefix >= beginPattern; --endPrefix)
if( *endPrefix == ':' || *endPrefix == '\\' )
break;
dirNameSize = endPrefix - beginPattern + 1;
/* start looking for matching file names */
fileName = findFirstFile(beginPattern, savedAttribute, dta);
/* skip "." */
if( fileName == NULL || strcmp(&fileName[30], ".") == 0 )
return findNextPatternFile(dta);
/* else contruct the filename */
for(i = 0; i < dirNameSize; ++i)
retFilename[i] = beginPattern[i];
retFilename[i] = '\0';
strcat(retFilename, &fileName[30]);
return retFilename;
}
unsigned char * pascal
/* XTAG:findNextPatternFile */
findNextPatternFile(dta)
unsigned char *dta;
{
unsigned char *fileName;
unsigned char *endPrefix;
register int i;
/* find the next matching file name */
fileName = findNextFile(dta);
if( fileName != NULL )
goto ret;
tryMorePatterns:
/* see if this pattern has two or more components left */
if( endPattern != NULL )
*endPattern++ = '|'; /* restore pattern as it was */
else /* no more file patterns to expand */
return NULL;
/* next pattern begin where the last one ends */
beginPattern = endPattern;
endPattern = strchr(beginPattern, '|');
if( endPattern != NULL ) {
*endPattern = '\0';
endPrefix = endPattern - 1;
} else
endPrefix = beginPattern + strlen(beginPattern) - 1;
for( ; endPrefix >= beginPattern; --endPrefix)
if( *endPrefix == ':' || *endPrefix == '\\' )
break;
dirNameSize = endPrefix - beginPattern + 1;
/* start looking for matching file names */
fileName = findFirstFile(beginPattern, savedAttribute, dta);
if( fileName == NULL )
goto tryMorePatterns;
ret:
/* else contruct the filename */
for(i = 0; i < dirNameSize; ++i)
retFilename[i] = beginPattern[i];
retFilename[i] = '\0';
strcat(retFilename, &fileName[30]);
return retFilename;
}
unsigned char * pascal
/* XTAG:makeFullPathname */
makeFullPathname(origName)
unsigned char *origName;
{
extern unsigned char msgBuffer[];
extern unsigned char scratchFileName[];
int offset, n;
register unsigned char *p;
unsigned char *fromPtr, *toPtr, ch;
/* figure out the complete pathname for the file */
offset = 0; /* offset into origName */
/* first figure out what we have to do */
if( origName[1] == ':' ) {
if( origName[2] != '\\' && origName[2] != '/' ) {
/* get the current directory on that drive */
n = getDefaultDrive(); /* save default drive */
(void)setDefaultDrive(toupper(origName[0])-'A');
/* get the current directory on that drive */
(void)getcwd(&msgBuffer[0], MSGBUFFERSIZE);
/* restore the orignal defualt drive */
(void)setDefaultDrive(n);
/* skip the drive spec since getcwd gets it */
offset = 2;
goto fixDirectoryName;
} else /* name begins A:\ or something like it so */
/* just use the string typed in */
scratchFileName[0] = '\0';
} else if( origName[0] == '\\' || origName[0] == '/' ) {
/* add a drive identifier to the string typed in */
scratchFileName[0] = (unsigned char)'A' + getDefaultDrive();
scratchFileName[1] = ':';
scratchFileName[2] = '\0';
} else { /* must prepend the current drive and directory */
(void)getcwd(&msgBuffer[0], MSGBUFFERSIZE);
fixDirectoryName:
strcpy(scratchFileName, msgBuffer);
n = strlen(scratchFileName);
if( scratchFileName[n-1] != '\\' ) {
scratchFileName[n++] = '\\';
scratchFileName[n] = '\0';
}
}
strncat(scratchFileName, &(origName[offset]), FILENAMESIZE);
/* now eliminate any ".." components */
p = &scratchFileName[0];
while( *p != '\0' ) {
/* look for a "\..\" */
if( *p == '.' && *(p+1) == '.'
&& *(p-1) == '\\' && *(p+2) == '\\' ) {
/* find the previous path component */
n = 2;
while( 1 ) {
if( (p-n) < &scratchFileName[0] )
/* string is "component\..\ ..." */
break;
ch = *(p-n);
if( ch == '\\' || ch == ':' )
break;
++n;
}
/* eliminate the "component\..\" by copying the */
/* rest of the string up n character positions */
fromPtr = p + 3;
/* *(p-n) is the last character to keep so: */
toPtr = p - n + 1;
/* move p to continue the scan at the beginning */
/* of the moved part of the string */
p = toPtr;
while( 1 ) {
*toPtr = *fromPtr++;
if( *toPtr++ == '\0' )
break;
}
} else
++p;
}
return &scratchFileName[0];
}
struct window * pascal
/* XTAG:findFilenameWindow */
findFilenameWindow(filename)
unsigned char *filename;
{
extern struct window *windowList;
extern struct openFile *files;
register struct window *w;
w = windowList;
while( w != NULL ) {
if( stricmp(files[w->fileId].origName, filename) == 0 )
return w;
w = w->nextWindow;
}
return NULL;
}
unsigned char * pascal
/* XTAG:findFirstFile */
findFirstFile(pattern, attribute, dta)
unsigned char *pattern;
int attribute;
unsigned char *dta;
{
extern union REGS rin, rout;
/* set the disk tranfer address */
rin.h.ah = 0x1A;
rin.x.dx = (int)dta;
intdos(&rin, &rout);
/* start looking for matching file names */
rin.h.ah = 0x4E;
rin.x.cx = attribute; /* search attribute */
rin.x.dx = (int)pattern;
intdos(&rin, &rout);
if( (rout.x.cflag & 1) == 1 )
return NULL;
else
return dta;
}
unsigned char * pascal
/* XTAG:findNextFile */
findNextFile(dta)
unsigned char *dta;
{
extern union REGS rin, rout;
/* set the disk tranfer address */
rin.h.ah = 0x1A;
rin.x.dx = (int)dta;
intdos(&rin, &rout);
/* find the next matching file name */
rin.h.ah = 0x4F;
intdos(&rin, &rout);
if( (rout.x.cflag & 1) == 1 )
return NULL;
else
return dta;
}
int pascal
/* XTAG:findTag */
findTag(fn)
int fn;
{
extern struct window *selWindow;
extern long selBegin, selEnd;
extern int selMode;
extern unsigned char msgBuffer[];
extern unsigned char textBuffer[];
extern unsigned char tagPattern[];
extern unsigned char tagMarker[];
extern unsigned char lastTag[STRINGSIZE];
extern unsigned char tagDTA[64];
extern long tagLastCp;
extern int tagLastLine;
extern int scrRows, scrCols;
extern int menuLine;
extern int ignoreCase;
struct window *w;
register unsigned char *p;
unsigned char tagString[STRINGSIZE], *filename, ch;
int fileId, linesPassed, markerLength;
long cp, fSize, startCp;
int row1, row2, col1, col2;
int ret, saveIgnoreCase, totalLinesPassed;
/* get the string to search for */
if( fn == FTAGSEL || fn == FCTAGSEL ) {
getSelection(tagString);
} else { /* fn = FTAG || fn == FCTAG */
filename = getInput("Tag name: ", lastTag, 0);
if( filename == NULL ) {
msg("Find tag cancelled", 1);
ret = 2;
goto doReturn;
}
strncpy(tagString, filename, STRINGSIZE);
}
/* adjust for ignoring case by forcing all lower case */
if( ignoreCase != 0 ) {
p = tagString